package com.music.note_android.ui.folder

import android.app.Dialog
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import com.google.android.material.button.MaterialButton
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import com.music.note_android.R

class CreateFolderDialog : DialogFragment() {

    private lateinit var tilFolderName: TextInputLayout
    private lateinit var etFolderName: TextInputEditText
    private lateinit var btnCancel: MaterialButton
    private lateinit var btnConfirm: MaterialButton
    
    private var onFolderCreated: ((String) -> Unit)? = null

    companion object {
        fun newInstance(onFolderCreated: (String) -> Unit): CreateFolderDialog {
            return CreateFolderDialog().apply {
                this.onFolderCreated = onFolderCreated
            }
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_create_folder, null)
        
        initViews(view)
        setupListeners()
        
        return AlertDialog.Builder(requireContext())
            .setView(view)
            .create()
    }

    private fun initViews(view: View) {
        tilFolderName = view.findViewById(R.id.til_folder_name)
        etFolderName = view.findViewById(R.id.et_folder_name)
        btnCancel = view.findViewById(R.id.btn_cancel)
        btnConfirm = view.findViewById(R.id.btn_confirm)
        
        // 初始状态下确认按钮不可用
        btnConfirm.isEnabled = false
    }

    private fun setupListeners() {
        // 文本变化监听
        etFolderName.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                validateInput()
            }
            
            override fun afterTextChanged(s: Editable?) {}
        })
        
        // 取消按钮
        btnCancel.setOnClickListener {
            dismiss()
        }
        
        // 确认按钮
        btnConfirm.setOnClickListener {
            val folderName = etFolderName.text?.toString()?.trim()
            if (!folderName.isNullOrEmpty() && validateFolderName(folderName)) {
                onFolderCreated?.invoke(folderName)
                dismiss()
            }
        }
    }

    private fun validateInput() {
        val folderName = etFolderName.text?.toString()?.trim()
        
        when {
            folderName.isNullOrEmpty() -> {
                tilFolderName.error = null
                btnConfirm.isEnabled = false
            }
            !validateFolderName(folderName) -> {
                btnConfirm.isEnabled = false
            }
            else -> {
                tilFolderName.error = null
                btnConfirm.isEnabled = true
            }
        }
    }

    private fun validateFolderName(name: String): Boolean {
        return when {
            name.isEmpty() -> {
                tilFolderName.error = "文件夹名称不能为空"
                false
            }
            name.length > 50 -> {
                tilFolderName.error = "文件夹名称不能超过50个字符"
                false
            }
            name.contains("/") || name.contains("\\") -> {
                tilFolderName.error = "文件夹名称不能包含特殊字符"
                false
            }
            name.trim() != name -> {
                tilFolderName.error = "文件夹名称首尾不能有空格"
                false
            }
            else -> {
                tilFolderName.error = null
                true
            }
        }
    }

    override fun onStart() {
        super.onStart()
        // 显示软键盘
        etFolderName.requestFocus()
    }
}